/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 */
package com.hyyt.shopjsp.wxmg.basicInfo.service.imp;

import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.wxmg.basicInfo.dao.WxMenuInfoDAO;
import com.hyyt.shopjsp.wxmg.basicInfo.pojo.WxMenuInfo;
import com.hyyt.shopjsp.wxmg.basicInfo.service.IWxMenuInfoService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator on 2017/11/10.
 *
 * WX_微信菜单信息
 */
@Service
public class WxMenuInfoService implements IWxMenuInfoService {
    private static  final Logger logger = Logger.getLogger(IWxMenuInfoService.class);

    @Resource
    private WxMenuInfoDAO wxMenuInfoDAO;

    /**
     * 保存WX_微信菜单信息
     *
     * @param wxMenuInfo WX_微信菜单信息
     * @return 保存后的WX_微信菜单信息
     */
    @Override
    public void saveWxMenuInfo(WxMenuInfo wxMenuInfo) {
        wxMenuInfoDAO.insert(wxMenuInfo);
    }

    /**
     * 更新WX_微信菜单信息
     *
     * @param wxMenuInfo WX_微信菜单信息
     * @return 更新后的WX_微信菜单信息
     */
    @Override
    public void updateWxMenuInfo(WxMenuInfo wxMenuInfo) {
        wxMenuInfoDAO.update(wxMenuInfo);
    }

    /**
     * 删除指定ID的WX_微信菜单信息
     *
     * @param id WX_微信菜单信息ID
     */
    @Override
    public void deleteWxMenuInfo(Integer id) {
        wxMenuInfoDAO.delete(id);
    }


    /**
     * 根据ID加载一个WX_微信菜单信息
     *
     * @param id WX_微信菜单信息ID
     * @return WX_微信菜单信息
     */
    @Override
    public WxMenuInfo loadWxMenuInfo(Integer id) {
        return wxMenuInfoDAO.load(id);
    }

    /**
    * 查询所有的WX_微信菜单信息，通常有最多取30000条记录的限制
    *
    * @return 所有的WX_微信菜单信息，通常有最多取30000条记录的限制
    */
    @Override
    public List<WxMenuInfo> queryAllWxMenuInfo() {
        List <WxMenuInfo> allWxMenuInfoList=wxMenuInfoDAO.queryAll();
        List <WxMenuInfo> firstWxMenuInfoList=null;
        List <WxMenuInfo> sencondWxMenuInfoList=null;
        if(Utils.objectIsNotEmpty(allWxMenuInfoList)){
            firstWxMenuInfoList=new ArrayList<WxMenuInfo>();
            for(WxMenuInfo all_wmi:allWxMenuInfoList){//寻找一级节点
                if(all_wmi.getParentId().intValue()==0){//表示一级节点
                    firstWxMenuInfoList.add(all_wmi);
                }
            }
            for(WxMenuInfo first_wmi:firstWxMenuInfoList){//寻找一级节点下的二级节点并归类
                sencondWxMenuInfoList=new ArrayList<WxMenuInfo>();
                for(WxMenuInfo second_wmi:allWxMenuInfoList){
                    if(second_wmi.getParentId().intValue()==first_wmi.getWxmiId().intValue()){//表示一级节点
                        sencondWxMenuInfoList.add(second_wmi);
                    }
                }
                first_wmi.setSubWxMenuList(sencondWxMenuInfoList);
            }
        }
        return firstWxMenuInfoList;
    }

    /**
     * 查询所有的WX_微信菜单信息返回集合结果，通常有最多取30000条记录的限制
     *
     * @return 所有的WX_微信菜单信息返回集合结果，通常有最多取30000条记录的限制
     */
    @Override
    public List<WxMenuInfo> queryAllWxMenuList(){
        List <WxMenuInfo> allWxMenuInfoList = wxMenuInfoDAO.queryAll();
        return allWxMenuInfoList;
    }

    /**
     * 查询WX_微信菜单信息
     *
     * @param params 查询参数，为key-value形式
     * @return WX_微信菜单信息集合
     */
    @Override
    public List<WxMenuInfo> queryWxMenuInfo(Map<String, Object> params) {
        return wxMenuInfoDAO.query(params);
    }

    /**
     * 统计微信一级菜单的数量
     *
     * @param params 查询参数，没有则传null,分页参数key为“sortColumns”
     * @return 结果集记录数
     */
    @Override
    public int countWxMenuInfo(Map<String, Object> params) {
        return wxMenuInfoDAO.count(params);
    }

    /**
     * 统计微信二级菜单的数量
     *
     * @param params 查询参数，没有则传null,分页参数key为“sortColumns”
     * @return 结果集记录数
     */
    @Override
    public int countWxSecondMenuInfo(Map<String, Object> params) {
        return wxMenuInfoDAO.count("countWxSecondMenuInfo",params);
    }

    /**
     * 组装菜单的json数据
     * @param wxMenuInfoList
     * @return
     *		 {"button":[
     *	      {"type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC"},
     *	      {
     *	           "name":"菜单",
     *	           "sub_button":[
     *	            { "type":"view","name":"搜索",url":"http://www.soso.com/"},
     *	            { "type":"click","name":"赞一下我们","key":"V1001_GOOD"}
     *		       ]
     *	       }
     *	      ]}
     * {
     * "button":[
     * {	"type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC"},
     * {
     *  "name":"菜单",
     * "sub_button":[
     *  {"type":"view","name":"搜索", "url":"http://www.soso.com/"},
     *  {"type":"view","name":"视频","url":"http://v.qq.com/"},
     *  {"type":"click", "name":"赞一下我们","key":"V1001_GOOD"}
     *  ]
     *  }]
     * }
     */
    public String getMenuJsonString(List <WxMenuInfo> wxMenuInfoList){
        StringBuilder sb=new StringBuilder();
        StringBuilder firstsb=new StringBuilder();
        StringBuilder secondsb=new StringBuilder();
        if(wxMenuInfoList!=null&&wxMenuInfoList.size()>0){
            sb.append("{ \"button\":[ ");
            for(WxMenuInfo firstWXMenuInfo:wxMenuInfoList){
                if(firstWXMenuInfo.getParentId().intValue()==0){//一级菜单
                    //resortList.add(firstWXMenuInfo);
                    firstsb.append("{");
                    if(firstWXMenuInfo.getIsOwnerSecondLevel().intValue()==0){//表示当前一级菜单没有二级菜单
                        firstsb.append("\"type\":\""+firstWXMenuInfo.getType()+"\",");
                        firstsb.append("\"name\":\""+firstWXMenuInfo.getName()+"\",");
                        if("click".equals(firstWXMenuInfo.getType())){
                            firstsb.append("\"key\":\""+firstWXMenuInfo.getTypeValue()+"\"");
                        }else if("view".equals(firstWXMenuInfo.getType())){
                            firstsb.append("\"url\":\""+firstWXMenuInfo.getTypeValue()+"\"");
                        }
                    }else if(firstWXMenuInfo.getIsOwnerSecondLevel().intValue()==1){//表示当前一级菜单具有二级菜单
                        firstsb.append("\"name\":\""+firstWXMenuInfo.getName()+"\",");
                        firstsb.append("\"sub_button\":[");
                        secondsb=new StringBuilder();//重新创建，否则后面会有叠加重复
                        for(WxMenuInfo secondWXMenuInfo:wxMenuInfoList){
                            //当前二级菜单是当前一级菜单的子菜单
                            if(firstWXMenuInfo.getWxmiId().intValue()==secondWXMenuInfo.getParentId().intValue()){//二级菜单
                                secondsb.append("{");
                                secondsb.append("\"type\":\""+secondWXMenuInfo.getType()+"\",");
                                secondsb.append("\"name\":\""+secondWXMenuInfo.getName()+"\",");
                                if("click".equals(secondWXMenuInfo.getType())){
                                    secondsb.append("\"key\":\""+secondWXMenuInfo.getTypeValue()+"\"");
                                }else if("view".equals(secondWXMenuInfo.getType())){
                                    secondsb.append("\"url\":\""+secondWXMenuInfo.getTypeValue()+"\"");
                                }
                                secondsb.append("},");
                            }
                        }
                        String secondjsonString=secondsb.toString();
                        if(Utils.objectIsNotEmpty(secondjsonString)) {
                            secondjsonString=secondjsonString.substring(0,secondjsonString.length()-1);
                        }
                        firstsb.append(secondjsonString);
                        firstsb.append("]");
                    }
                    firstsb.append("},");
                }
            }
            String firstjsonString=firstsb.toString();
            if(Utils.objectIsNotEmpty(firstjsonString)) {
                firstjsonString = firstjsonString.substring(0, firstjsonString.length() - 1);
            }
            sb.append(firstjsonString);
            sb.append("]}");
        }else{
            sb.append("");
        }
        return sb.toString();
    }
    /*public String getMenuJsonString(List <WxMenuInfo> wxMenuInfoList){
        StringBuilder sb=new StringBuilder();
        StringBuilder firstsb=new StringBuilder();
        StringBuilder secondsb=new StringBuilder();
        if(wxMenuInfoList!=null&&wxMenuInfoList.size()>0){
            sb.append("{ \"button\":[ ");
            for(WxMenuInfo firstWXMenuInfo:wxMenuInfoList){
                if(firstWXMenuInfo.getParentId().intValue()==0){//一级菜单
                    //resortList.add(firstWXMenuInfo);
                    firstsb.append("{");
                    if(firstWXMenuInfo.getIsOwnerSecondLevel().intValue()==0){//表示当前一级菜单没有二级菜单
                        firstsb.append("\"type\":\""+firstWXMenuInfo.getType()+"\",");
                        firstsb.append("\"name\":\""+firstWXMenuInfo.getName()+"\",");
                        if("click".equals(firstWXMenuInfo.getType())){
                            firstsb.append("\"key\":\""+firstWXMenuInfo.getTypeValue()+"\"");
                        }else if("view".equals(firstWXMenuInfo.getType())){
                            firstsb.append("\"url\":\""+firstWXMenuInfo.getTypeValue()+"\"");
                        }

                    }else if(firstWXMenuInfo.getIsOwnerSecondLevel().intValue()==1){//表示当前一级菜单具有二级菜单
                        firstsb.append("\"name\":\""+firstWXMenuInfo.getName()+"\",");
                        firstsb.append("\"sub_button\":[");
                        secondsb=new StringBuilder();//重新创建，否则后面会有叠加重复
                        for(WxMenuInfo secondWXMenuInfo:wxMenuInfoList){
                            //当前二级菜单是当前一级菜单的子菜单
                            if(firstWXMenuInfo.getWxmiId().intValue()==secondWXMenuInfo.getParentId().intValue()){//二级菜单
                                secondsb.append("{");
                                secondsb.append("\"type\":\""+secondWXMenuInfo.getType()+"\",");
                                secondsb.append("\"name\":\""+secondWXMenuInfo.getName()+"\",");
                                if("click".equals(secondWXMenuInfo.getType())){
                                    secondsb.append("\"key\":\""+secondWXMenuInfo.getTypeValue()+"\"");
                                }else if("view".equals(secondWXMenuInfo.getType())){
                                    secondsb.append("\"url\":\""+secondWXMenuInfo.getTypeValue()+"\"");
                                }
                                secondsb.append("},");
                            }
                        }
                        String secondjsonString=secondsb.toString();
                        if(Utils.objectIsNotEmpty(secondjsonString)) {
                            secondjsonString=secondjsonString.substring(0,secondjsonString.length()-1);
                        }
                        firstsb.append(secondjsonString);
                        firstsb.append("]");
                    }
                    firstsb.append("},");
                }
            }
            String firstjsonString=firstsb.toString();
            if(Utils.objectIsNotEmpty(firstjsonString)) {
                firstjsonString = firstjsonString.substring(0, firstjsonString.length() - 1);
            }
            sb.append(firstjsonString);
            sb.append("]}");
        }else{
            sb.append("");
        }
        return sb.toString();
    }*/

}